#!/usr/bin/env python
# -*- coding: utf-8  -*-

# This file is part of an old version of the Opuntia's lightweight ESTdb.
# This code is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Izaskun Mallona
# izaskun.mallona@gmail.com

from Cheetah.Template import Template
import csv, cgi, re
from html import *
from string import Template

#######
#['UniProtKB', 'A0A078', 'ECA2', '', 'GO:0015662', 'GO_REF:0000002', 'IEA', 'InterPro:IPR018303', 'F', 'Endomembrane Ca2+ ATPase 2', 'ECA2|At4g00900|IPI00524443|A0A078_ARATH', 'protein', 'taxon:3702', '20100717', 'InterPro']
goaInterPRO = Template("""
    <b><a href="http://www.uniprot.org/uniprot/$UniProtKBID">$UniProtKBID</a></b>
($symbol). 
<a href="http://amigo.geneontology.org/cgi-bin/amigo/term-details.cgi?term=$GONumber">$GONumber</a>
($GORef, $basis)
<a href="http://www.ebi.ac.uk/interpro/IEntry?ac=$InterPRO">$InterPRO</a>: 
<b>$annotation</b>. 
<a href="http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=$taxon">Taxon: $taxon</a>

""")
#######
#['UniProtKB', 'A0A078', 'ECA2', '', 'GO:0015662', 'GO_REF:0000002', 'IEA', 'InterPro:IPR018303', 'F', 'Endomembrane Ca2+ ATPase 2', 'ECA2|At4g00900|IPI00524443|A0A078_ARATH', 'protein', 'taxon:3702', '20100717', 'InterPro']
goaExpasy = Template("""
    <b><a href="http://www.uniprot.org/uniprot/$UniProtKBID">$UniProtKBID</a></b>
($symbol). 
<a href="http://amigo.geneontology.org/cgi-bin/amigo/term-details.cgi?term=$GONumber">$GONumber</a>
($GORef, $basis)
<a href ="http://expasy.org/cgi-bin/get-entries?KW=$expasy">$expasy</a>
<b>$annotation</b>. 
<a href="http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=$taxon">Taxon: $taxon</a>

""")
#########

form = cgi.FieldStorage()
query = form.getvalue('query')
pattern = re.compile("lcl|"+query)

reader = csv.reader(open('drought.csv'), delimiter='\t')
data = list(reader)

found = False
i=1

htmlUp()

# remove and i statement
while not found and i < len(data):
    if "lcl|"+query == data[i][1]:
        print """The UniProtKB id <b>%s</b> matches the Opuntia accession
<b><a href="http://srvgen.upct.es/cgi-bin/opuntia/fetch.cgi?identifiers=%s">%s</a></b> you queried with an e-value of
<b>%s</b><br><br>""" %(data[i][0], data[i][1][4:], data[i][1], data[i][2])

        found = True
        UniProtKB = data[i][0]
    i+=1
if not found:
    print "Query not found"
else:
    goaReader = csv.reader(open('drought_stress.goa'), delimiter='\t')
    goa = list(goaReader)
    toParse= UniProtKB[UniProtKB.find("|")+1:]
    UniProtKB= toParse[:toParse.find("|")]
    pattern = re.compile(UniProtKB)

    found = False
    i = 0
    while not found and i<(len(goa)-7):
        #print "the uniprotkb is "+UniProtKB
        #print goa[6+i][1] +"    " + UniProtKB+"<br>"
        if UniProtKB== goa[6+i][1]:
            print "The Gene Association of that UniProtKB accession is the following:<br><br>"
            if re.match("InterPro", goa[6+i][7]):
                print goaInterPRO.substitute(UniProtKBID=
                    goa[6+i][1], 
                    symbol=goa[6+i][2], 
                    GONumber=goa[6+i][4], 
                    GORef=goa[6+i][5], 
                    basis=goa[6+i][6], 
                    InterPRO=goa[6+i][7][9:],
                    annotation=goa[6+i][9],
                    taxon=goa[6+i][12][6:]
                    )
            else:
                print goaExpasy.substitute(UniProtKBID=
                    goa[6+i][1], 
                    symbol=goa[6+i][2], 
                    GONumber=goa[6+i][4], 
                    GORef=goa[6+i][5], 
                    basis=goa[6+i][6], 
                    expasy=goa[6+i][7][6:],
                    annotation=goa[6+i][9],
                    taxon=goa[6+i][12][6:]
                    )
                
            print "<br><br>"
            #print str(goa[6+i])+"<br><br>"
            found = True
        else:
                        i+=1
    if found:
        while UniProtKB == goa[6+i][1] and i<(len(goa)-7):
            if re.match("InterPro", goa[6+i][7]):
                print goaInterPRO.substitute(UniProtKBID=
                    goa[6+i][1], 
                    symbol=goa[6+i][2], 
                    GONumber=goa[6+i][4], 
                    GORef=goa[6+i][5], 
                    basis=goa[6+i][6], 
                    InterPRO=goa[6+i][7][9:],
                    annotation=goa[6+i][9],
                    taxon=goa[6+i][12][6:]
                    )
            else:
                print goaExpasy.substitute(UniProtKBID=
                    goa[6+i][1], 
                    symbol=goa[6+i][2], 
                    GONumber=goa[6+i][4], 
                    GORef=goa[6+i][5], 
                    basis=goa[6+i][6], 
                    expasy=goa[6+i][7][6:],
                    annotation=goa[6+i][9],
                    taxon=goa[6+i][12][6:]
                    )
            print "<br><br>"
            i+=1

htmlBottom()
